package com.aistudy.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.aistudy.common.Result;
import com.aistudy.dto.request.LoginRequest;
import com.aistudy.dto.request.RegisterRequest;
import com.aistudy.dto.response.LoginResponse;
import com.aistudy.entity.User;
import com.aistudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest request) {
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(request.getPassword());
        user.setEmail(request.getEmail());
        
        boolean success = userService.register(user);
        return success ? Result.success() : Result.error("注册失败");
    }

    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody LoginRequest request) {
        User user = userService.login(request.getUsername(), request.getPassword());
        
        LoginResponse response = new LoginResponse();
        response.setId(user.getId());
        response.setUsername(user.getUsername());
        response.setEmail(user.getEmail());
        response.setRole(user.getRole());
        
        return Result.success(response);
    }
}